package com.project.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.project.bean.DiscountBean;
import com.project.service.IPromotionalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("promotional")
public class promotionalController {
    @Autowired
    private IPromotionalService promotionalService;
    //前端掉这个方法得到静态页面
    @RequestMapping("getPromotional")
    public String getPromotional(HttpServletRequest request, Model model){
            List<DiscountBean> listDiscount=promotionalService.findAllByStatus();
        model.addAttribute("listDiscount",listDiscount);
        return "promotional.html";
    }

@PostMapping("/addToCart")
@ResponseBody
public String addToCart(@RequestBody JSONObject discount) {
    JSONObject params = discount.getJSONObject("params");
    String discountStr = params.getString("discount");
//得到促销商品对象以及信息
    DiscountBean discountBean = JSON.parseObject(discountStr, DiscountBean.class);
//    // 使用 DiscountBean 对象的属性信息
    System.out.println(discountBean.getShopName());
    System.out.println(discountBean.getOrigina());
    System.out.println(discountBean.getType());

    return "OK";
}
}
